package com.aiti.ai.service.impl.ai;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.aiti.ai.dto.ai.BuildDataDto;
import com.aiti.ai.entity.ShowPremuEnterpriseEntity;
import com.aiti.ai.mapper.ShowPremuEnterpriseMapper;
import com.aiti.ai.service.ShowPremuEnterpriseService;
import com.aiti.ai.service.ai.MessageAssistant;
import com.aiti.ai.task.MainTaskRunnable;
import com.aiti.ai.tool.SearXNGWebSearchTool;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.service.AiServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TestServiceImpl1 {
    @Resource
    private SearXNGWebSearchTool searXNGWebSearchTool;
    @Autowired
    private ChatLanguageModel chatLanguageModel;
    @Autowired
    private ShowPremuEnterpriseService showPremuEnterpriseService;
    @Autowired
    private ShowPremuEnterpriseMapper showPremuEnterpriseMapper;

    public String testAiResponse(String enterpriseName) {
        MessageAssistant assistant = AiServices.builder(MessageAssistant.class)
                .chatLanguageModel(chatLanguageModel)
                .tools(searXNGWebSearchTool).build();

        return assistant.dealExistField(enterpriseName);
    }

    public void startTask() {
        new Thread(new MainTaskRunnable(showPremuEnterpriseService, chatLanguageModel, searXNGWebSearchTool)).start();
    }

    public void buildData(BuildDataDto dto) {
        if(CollectionUtil.isEmpty(dto.getValues())) {
            return ;
        }
        int i = RandomUtil.randomInt(0, dto.getValues().size() - 1);
        List<String> ids = showPremuEnterpriseMapper.queryUnDealIds(dto.getColumn());
        if(CollectionUtil.isEmpty(ids)) {
            return ;
        }
        for(String id: ids) {
            showPremuEnterpriseMapper.updatecolumn(id, dto.getColumn(), dto.getValues().get(i));
        }

    }


}
